﻿using Microsoft.OpenApi.Models;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;
using Volo.Abp.Swashbuckle;

namespace AbpAPI.Web
{
    [DependsOn(typeof(AbpAPIApplicationModule))]
    [DependsOn(typeof(AbpAspNetCoreMvcModule))]
    [DependsOn(typeof(AbpSwashbuckleModule))]
    [DependsOn(typeof(AbpAutofacModule))]
    public class AbpAPIWebModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            base.ConfigureServices(context);
            //注入自动API
            Configure<AbpAspNetCoreMvcOptions>(options =>
            {
                options
                    .ConventionalControllers
                    .Create(typeof(AbpAPIApplicationModule).Assembly);  //设置哪个类库要自动API，可配置多个服务实现层
            });

            //下面是自动生成swaggerUI
            var services = context.Services;
            services.AddAbpSwaggerGen(options =>
        {
            options.SwaggerDoc("v1", new OpenApiInfo { Title = "动态API", Version = "v1" });
            options.DocInclusionPredicate((docName, description) => true);
            options.CustomSchemaIds(type => type.FullName);

        }
    );

        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            
            var app = context.GetApplicationBuilder();

            //应用swaggerUI
            app.UseSwagger();
            app.UseAbpSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "动态API");
            });

            base.OnApplicationInitialization(context);
        }
    }
}
